package com.videoweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.videoweb.entity.VideoLike;
import com.videoweb.mapper.VideoLikeMapper;
import com.videoweb.service.IVideoLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.videoweb.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmr
 * @since 2025-05-17
 */
@Service
public class VideoLikeServiceImpl extends ServiceImpl<VideoLikeMapper, VideoLike> implements IVideoLikeService {

    @Autowired
    VideoLikeMapper videoLikeMapper;

    @Override
    public boolean checkLike(Long videoId) {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<VideoLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoLike::getVideoId, videoId)
                    .eq(VideoLike::getUserId, userId);
        // 查询用户是否点赞
        Long row = videoLikeMapper.selectCount(queryWrapper);
        return row > 0;
    }

    @Override
    public boolean likeIt(Long videoId) {
        Long userId = SecurityUtils.getUserId();

        VideoLike videoLike = new VideoLike()
                .setVideoId(videoId)
                .setUserId(userId);
        int rows= videoLikeMapper.insert(videoLike);
        return rows>0;
    }

    @Override
    public boolean cancelLike(Long videoId) {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<VideoLike> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoLike::getVideoId,videoId)
                .eq(VideoLike::getUserId,userId);

        int rows = this.videoLikeMapper.delete(queryWrapper);
        return rows>0 ;
    }
}
